const CacheManager = require("../template")

function testCacheManagerCacheOperations() {
  const cache = new CacheManager(10)

  console.log(
    "1. TTL过期测试:",
    (() => {
      cache.set('ttl1', 'value1', 100) // 100ms过期
      const beforeExpire = cache.get('ttl1')
      
      // 等待过期
      return new Promise((resolve) => {
        setTimeout(() => {
          const afterExpire = cache.get('ttl1')
          resolve(beforeExpire === 'value1' && afterExpire === undefined)
        }, 150)
      })
    })()
  )

  console.log(
    "2. LRU淘汰测试:",
    (() => {
      cache.setMaxSize(3)
      cache.clear()
      
      cache.set('lru1', 'value1')
      cache.set('lru2', 'value2')
      cache.set('lru3', 'value3')
      cache.set('lru4', 'value4') // 应该淘汰lru1
      
      const hasLru1 = cache.has('lru1')
      const hasLru4 = cache.has('lru4')
      
      return hasLru1 === false && hasLru4 === true
    })()
  )

  console.log(
    "3. 缓存更新测试:",
    (() => {
      cache.set('update1', 'value1')
      cache.set('update1', 'value2') // 更新
      const value = cache.get('update1')
      return value === 'value2'
    })()
  )

  console.log(
    "4. 缓存命中率测试:",
    (() => {
      cache.clear()
      cache.set('hit1', 'value1')
      cache.get('hit1') // 命中
      cache.get('hit1') // 命中
      cache.get('hit2') // 未命中
      
      const stats = cache.getStats()
      return stats.hits === 2 && stats.misses === 1
    })()
  )

  console.log(
    "5. 缓存清理测试:",
    (() => {
      cache.clear()
      cache.set('clean1', 'value1', 50)
      cache.set('clean2', 'value2', 100)
      cache.set('clean3', 'value3', 200)
      
      const beforeCleanup = cache.size()
      
      // 等待部分过期
      return new Promise((resolve) => {
        setTimeout(() => {
          const cleaned = cache.cleanup()
          const afterCleanup = cache.size()
          resolve(beforeCleanup === 3 && cleaned >= 1 && afterCleanup < 3)
        }, 75)
      })
    })()
  )

  console.log(
    "6. 缓存批量操作测试:",
    (() => {
      cache.clear()
      
      // 批量设置
      for (let i = 0; i < 5; i++) {
        cache.set(`batch${i}`, `value${i}`)
      }
      
      // 批量获取
      let allFound = true
      for (let i = 0; i < 5; i++) {
        if (cache.get(`batch${i}`) !== `value${i}`) {
          allFound = false
          break
        }
      }
      
      return allFound === true && cache.size() === 5
    })()
  )

  console.log(
    "7. 缓存复杂数据类型测试:",
    (() => {
      cache.clear()
      
      const complexData = {
        obj: { a: 1, b: 2 },
        arr: [1, 2, 3],
        func: () => 'test',
        date: new Date()
      }
      
      cache.set('complex', complexData)
      const retrieved = cache.get('complex')
      
      return JSON.stringify(retrieved.obj) === JSON.stringify(complexData.obj) &&
             JSON.stringify(retrieved.arr) === JSON.stringify(complexData.arr) &&
             retrieved.func() === complexData.func() &&
             retrieved.date.getTime() === complexData.date.getTime()
    })()
  )

  console.log(
    "8. 缓存访问顺序测试:",
    (() => {
      cache.clear()
      cache.setMaxSize(3)
      
      cache.set('access1', 'value1')
      cache.set('access2', 'value2')
      cache.set('access3', 'value3')
      
      // 访问access1，使其成为最近访问
      cache.get('access1')
      
      // 添加新项，应该淘汰access2
      cache.set('access4', 'value4')
      
      const hasAccess1 = cache.has('access1')
      const hasAccess2 = cache.has('access2')
      const hasAccess3 = cache.has('access3')
      const hasAccess4 = cache.has('access4')
      
      return hasAccess1 === true &&
             hasAccess2 === false &&
             hasAccess3 === true &&
             hasAccess4 === true
    })()
  )

  console.log(
    "9. 缓存统计一致性测试:",
    (() => {
      cache.clear()
      
      cache.set('stats1', 'value1')
      cache.set('stats2', 'value2')
      cache.get('stats1')
      cache.get('stats3') // 不存在
      cache.delete('stats1')
      
      const stats = cache.getStats()
      
      return stats.sets === 2 &&
             stats.hits === 1 &&
             stats.misses === 1 &&
             stats.deletes === 1
    })()
  )

  console.log(
    "10. 缓存边界条件测试:",
    (() => {
      cache.clear()
      
      // 边界条件测试
      const boundaryTests = [
        () => cache.set('', 'empty'), // 空键
        () => cache.set('key', null), // null值
        () => cache.set('key', undefined), // undefined值
        () => cache.get('nonexistent'), // 不存在的键
        () => cache.delete('nonexistent'), // 删除不存在的键
        () => cache.has('nonexistent') // 检查不存在的键
      ]
      
      let successCount = 0
      boundaryTests.forEach(test => {
        try {
          test()
          successCount++
        } catch (error) {
          // 某些操作可能失败
        }
      })
      
      return successCount >= 4 // 至少4个操作成功
    })()
  )

  console.log(
    "11. this上下文缓存操作测试:",
    (() => {
      const cacheOpObj = {
        multiplier: 2,
        processCache: function(key, value) {
          const cache = new CacheManager()
          cache.set(key, value)
          return cache.get(key) * this.multiplier
        }
      }
      return cacheOpObj.processCache('test', 5) === 10
    })()
  )

  console.log(
    "12. 复杂对象this绑定缓存操作测试:",
    (() => {
      const cacheOpConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(key, value) {
          const cache = new CacheManager()
          cache.set(key, value)
          return cache.get(key) >= this.config.threshold
        }
      }
      return cacheOpConfigObj.processWithConfig('test', 5) === true
    })()
  )
}

testCacheManagerCacheOperations()
